package cn.phub.user.service.impl;

import cn.phub.basic.constant.BaseConstant;
import cn.phub.basic.dto.RegisterDto;
import cn.phub.basic.exception.MyException;
import cn.phub.basic.service.impl.BaseServiceImpl;
import cn.phub.basic.util.MD5Utils;
import cn.phub.basic.util.StrUtils;
import cn.phub.user.domain.Logininfo;
import cn.phub.user.domain.User;
import cn.phub.user.mapper.LogininfoMapper;
import cn.phub.user.mapper.UserMapper;
import cn.phub.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jijie
 * @since 2022-04-01
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public void phoneRegister(RegisterDto dto) {
        //校验
        String password = dto.getPassword();
        String smsCode = dto.getSmsCode();
        String confirmPassword = dto.getConfirmPassword();

        if (StringUtils.isEmpty(password)
                || StringUtils.isEmpty(smsCode)
                || StringUtils.isEmpty(confirmPassword)){
            throw new MyException("参数不能为空!");
        }
        //判断两次密码是否一致
        if (!dto.getPassword().equals(dto.getConfirmPassword())) {
            throw new MyException("密码不一致,请重新输入!");
        }
        //校验短信验证码
        String smsCodeKey = BaseConstant.SmsConstant.BUSINESS_REGISTER_PREFIX+dto.getPhone();
        Object o = redisTemplate.opsForValue().get(smsCodeKey);//9527:xxxxxxxx
        if (o == null) {
            throw new MyException("验证码过期,请重新获取!");
        }
        String sms = o.toString().split(":")[0];//9527
        if (o.toString().equalsIgnoreCase(dto.getSmsCode())) {
            throw new MyException("验证码错误");
        }
        //保存
        //初始化user
        User user = dto2User(dto);
        //将user拷贝成loginginfo
        Logininfo logininfo = user2Logininfo(user);
        //user+logininfo
        //返回自增ID
        logininfoMapper.save(logininfo);
        user.setLogininfoId(logininfo.getId());
        userMapper.save(user);
    }

    private Logininfo user2Logininfo(User user) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user,logininfo);
        //一定要设置
        logininfo.setType(1);
        return logininfo;
    }

    private User dto2User(RegisterDto dto){
        User user = new User();
        user.setUsername(dto.getPhone());
        user.setPhone(dto.getPhone());
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = MD5Utils.encrypByMd5(dto.getPassword() + salt);
        user.setSalt(salt);
        user.setPassword(md5Pwd);
        return user;
    }

    /**
     * 我们在CUD user表的时候，需要级联操作logininfo，所以我们需要复写 add update  remove
     */

    @Override
    public void add(User user) {
        super.add(user);
    }

    @Override
    public void update(User user) {
        super.update(user);
    }

    @Override
    public void remove(Long id) {
        super.remove(id);
    }
}
